package com.daydr3am.thenation;

import java.io.File;
import java.util.Vector;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;

import com.coe3.data.ExpandableHeightGridView;
import com.coe3.data.Information;
import com.coe3.data.PathManager;
import com.coe3.handle.ItemLibraryAvaliableAdapter;
import com.coe3.handle.ItemLibrarySpecialAdapter;

public class Library {

	private Activity mActivity;
	
	public static Library mInstance;
	
	public static Library getInstance() {
		if(mInstance == null)
			mInstance = new Library();
		
		return mInstance;
	}
	
	public void initial(Activity activity) {
		this.mActivity = activity;
		setDataSpecial();
		setDataAvaliable();
		setButtonOnline();
	}
	
	public void setButtonOnline() {
		mActivity.findViewById(R.id.lib_btn_online).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				TheNationMain.setIsOffline(mActivity, false);
				mActivity.finish();
				mActivity.startActivity(mActivity.getIntent());
			}
		});
	}
	
	public void setDataAvaliable() {
		try {
		    LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
			
		    ItemLibraryAvaliableAdapter adapter = new ItemLibraryAvaliableAdapter( mActivity, getPathAvaliable(), inflater );
			
		    ExpandableHeightGridView gridView = new ExpandableHeightGridView(mActivity);
		    gridView = (ExpandableHeightGridView) mActivity.findViewById(R.id.lib_grid_avaliable);
		    gridView.setAdapter(adapter);
		    gridView.setExpanded(true);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public void setDataSpecial() {
		try {
		    LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
			
		    ItemLibrarySpecialAdapter adapter = new ItemLibrarySpecialAdapter( mActivity, getPathSpeacial(), inflater );
			
		    ExpandableHeightGridView gridView = new ExpandableHeightGridView(mActivity);
		    gridView = (ExpandableHeightGridView) mActivity.findViewById(R.id.lib_grid_special);
		    gridView.setAdapter(adapter);
		    gridView.setExpanded(true);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public Vector<File> getPathAvaliable() {
		Vector<File> ret = new Vector<File>();
		File files = new File(PathManager.getExternalCacheDir(mActivity, false));
		for (File file : files.listFiles()) 
			ret.add(file);
		
		Information.logDEBUG("size: " + ret.size());
		
		return ret;
	}
	
	public Vector<String> getPathSpeacial() {
		Vector<String> ret = new Vector<String>();
		File files = new File(PathManager.getExternalCacheDir(mActivity, true));
		for (File file : files.listFiles()) 
			if(file.getAbsolutePath().endsWith(PathManager.Extension_JPG))
				ret.add(PathManager.removeExtension(file.getAbsolutePath()));
		
		return ret;
	}
}
